<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
        <el-form-item label="转移客户" prop="leaveAdminIds">
          <el-input v-model="formData.customerNames" disabled placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="目标员工" prop="ownerAdminIds">
          <el-select v-model="formData.ownerAdminIds" clearable multiple placeholder="选择用户" >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.nickname"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分配方式" prop="averageType">
          <el-radio-group v-model="formData.averageType">
            <el-radio :value="1">平均分配</el-radio>
            <el-radio :value="2">随机分配</el-radio>
          </el-radio-group>
        </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CustomerApi, CustomerVO } from '@/api/crm/crmcustomer'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as UserApi from '@/api/system/user'


/** 客户 表单 */
defineOptions({ name: 'CustomerForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('转移客户') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  customerNames: undefined,
  customerIds: undefined,
  ownerAdminIds: undefined,
  averageType: undefined
})
const formRules = reactive({
  ownerAdminIds: [{ required: true, message: '目标员工不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref
const userList = ref<UserApi.UserVO[]>([]) // 用户列表


/** 打开弹窗 */
const open = async (customers) => {
  resetForm()
  let namesArr = []
  let idsArr = []
  customers.forEach(value =>  {
    //console.log('value:',value)
    namesArr.push(value.name)
    idsArr.push(value.id)
  })
  formData.value.customerNames = namesArr.toString()
  formData.value.customerIds = idsArr
  //console.log('namesArr:',formData.value.customerNames)
  //console.log('idsArr:',idsArr)
  dialogVisible.value = true

  

  userList.value = await UserApi.getSimpleUserList()
 
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CustomerVO
    await CustomerApi.transferCustomer(data)
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}



/** 重置表单 */
const resetForm = () => {
  formData.value = {
    customerNames: undefined,
    customerIds: undefined,
    ownerAdminIds: undefined,
    averageType: 1
  }
  formRef.value?.resetFields()
}
</script>
<style>
/* .el-card__header {
  background-color:#66b1ff
} */
</style>